/*
 * @Author: wangming
 * @Date: 2021-03-26 10:45:02
 * @LastEditors: wangming
 * @LastEditTime: 2022-07-15 14:21:12
 * @Description: file content
 */
import Vue from 'vue'
import Router from 'vue-router'
import routes from 'vue-auto-routing'
import { createRouterLayout } from 'vue-router-layout'
import store from '@/store'
import { runApp } from './appRouters.js'

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => {
    return err
  })
}
const originalReplace = Router.prototype.replace
Router.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err)
}
Vue.use(Router)
const RouterLayout = createRouterLayout((layout) => {
  return import('@/layouts/' + layout + '.vue')
})
// name app- 开头对表示是app模式下对路由页面
const routers = new Router()
if (process.env.VUE_APP_USER === 'ADMIN') {
  const children = routes.filter((i) => !/^app-/.test(i.name))
  store.dispatch('generateMenus', children)
  store.dispatch('loadTenantUserInfo')
  routers.addRoutes(LayoutRoutes(children))
}
if (process.env.VUE_APP_USER === 'APP') {
  const children = routes.filter((i) => /^app-/.test(i.name))
  routers.addRoutes(LayoutRoutes(children))
  runApp(routers)
}
if (process.env.VUE_APP_ROOT_URL !== './') {
  console.log(JSON.parse(JSON.stringify(routes)))
}
export function LayoutRoutes(routes) {
  return [
    {
      path: '/',
      component: RouterLayout,
      children: routes
    }
  ]
}
routers.beforeEach(async (to, from, next) => {
  await store.dispatch("LOAD_APP_INFO");
  next();
});
routers.afterEach(async (to) => {
  /* 路由发生变化修改页面title */
  store.commit('SET_FULLPATH', to.fullPath)
})
export default routers

